package com.jyd.P1.controller;


import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jyd.P1.entity.Account;
import com.jyd.P1.enums.ResultEnum;
import com.jyd.P1.pojo.JsonResponse;
import com.jyd.P1.service.AccountService;
import org.springframework.boot.web.servlet.server.Jsp;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;

import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.UUID;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author jyd1124
 * @since 2023-07-20
 */
@Controller
public class AccountController {

    @Resource
    AccountService accountService;

//    @GetMapping("/add")
//    @ResponseBody
//    public JsonResponse<String> insertAccount () {
//        Account a = Account.builder()
//                .amount(10000L)
//                .accountId(UUID.randomUUID().toString())
//                .build();
//        accountService.insertAccount(a);
//        return JsonResponse.success();
//    }

    // 新增账户 - 开户
    @PostMapping("/add/{userId}")
    @ResponseBody
    public JsonResponse<String> insertAccount (@PathVariable Integer userId) {
        Account account1 = Account.builder()
                .accountId(UUID.randomUUID().toString())
                .userId(userId)
                .amount(0L)
                .build();
        String status = accountService.insertAccount(account1);
        if (ResultEnum.FAIL.getMsg().equals(status)) {
            return JsonResponse.fail("fail insert");
        }
        QueryWrapper<Account> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", userId);
        Account account2 = accountService.getOne(wrapper);
        return JsonResponse.success(account2.getAccountId());
    }

    // 注销账户 - 逻辑删除 logout
    @PostMapping("/logOut/{accountId}")
    @ResponseBody
    public JsonResponse<String> logOutAmountByAccountId (@PathVariable Integer accountId) {
        String status = accountService.logOutAmountByAccountId(accountId);
        if (ResultEnum.SUCCESS.getMsg().equals(status)) {
            return JsonResponse.success("logOut success");
        } else {
            return JsonResponse.fail("logOut fail");
        }
    }

    // 账户金额 - 转账
    @PostMapping("/transfer/{from}/{to}/{value}")
    @ResponseBody
    public JsonResponse<String> transfer (@PathVariable String from,
                                          @PathVariable String to,
                                          @PathVariable Long value) {
        String status = accountService.transfer(from, to, value);
        if (ResultEnum.SUCCESS.getMsg().equals(status)) {
            return JsonResponse.success("transfer success");
        } else {
            return JsonResponse.fail("transfer fail");
        }
    }

    // 查看剩余金额
    @PostMapping("/amount/{accounId}")
    public JsonResponse<String> getAmountByAccountId (@PathVariable Integer accounId) {
        return null;
    }

}

